package com.nw.serviceImpl;

import com.nw.dao.CommentDao;
import com.nw.entity.Comment;
import com.nw.service.CommentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

@Service
@Transactional
public class CommentServiceImpl implements CommentService {
    @Resource
    CommentDao commentDao;
    @Override
    public HashMap<String, Object> addComment(Comment comment) {
        HashMap<String, Object> map = new HashMap<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String nowTime = simpleDateFormat.format(date);
        try {
            comment.setCommentId(UUID.randomUUID().toString());
            try {
                Date nowTimeDate = simpleDateFormat.parse(nowTime);
                comment.setCommentTime(nowTimeDate);
            } catch (Exception e) {
                e.printStackTrace();
            }
            commentDao.addComment(comment);
            map.put("code","0");
            map.put("msg","添加评价成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code","1");
            map.put("msg","添加评价失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> deleteComment(Comment comment) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            commentDao.deleteComment(comment);
            map.put("code","0");
            map.put("msg","删除评价成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code","1");
            map.put("msg","删除评价失败");
        }
        return map;
    }
}
